//
//  RootViewController.m
//  GYStudyOC
//
//  Created by czera on 2020/8/6.
//  Copyright © 2020 czera. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,assign) BOOL isHaveDian;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
//    UILabel *label = [[UILabel alloc] init];
//    [self.view addSubview:label];
//    label.frame = CGRectMake(30, 100, self.view.frame.size.width-30*2, 40);
//    label.text = @"。你好吗XXXX、";
//
//    label.layer.borderColor = [UIColor greenColor].CGColor;
//    label.layer.borderWidth = 2;
    
    self.textField = [[UITextField alloc]init];
    self.textField.textColor =[UIColor blackColor];
    self.textField.textAlignment = NSTextAlignmentRight;
    self.textField.delegate = self;
    [self.textField addTarget:self action:@selector(textFieldsChange) forControlEvents:UIControlEventEditingDidEnd];
    [self.view addSubview:self.textField];
    
    
    self.textField.frame = CGRectMake(20, 100, self.view.frame.size.width-20*2, 50);
    self.textField.layer.borderColor = [UIColor redColor].CGColor;
    self.textField.layer.borderWidth = 2;
    self.textField.keyboardType = UIKeyboardTypeDecimalPad;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textField endEditing:YES];
}

- (void)textFieldsChange{
    
    if ([self.textField.text floatValue] > 99999) {
        self.textField.text = @"99999";
    }
//    if ([self.textttt.text floatValue] <=0) {
//        self.textttt.text = @"0";
//    }

    NSLog(@"- %@",self.textField.text);
}

/**
 *  textField的代理方法，监听textField的文字改变
 *  textField.text是当前输入字符之前的textField中的text
 *
 *  @param textField textField
 *  @param range     当前光标的位置
 *  @param string    当前输入的字符
 *
 *  @return 是否允许改变
 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.textField) {
        if ([textField.text intValue] > 99999) {
            self.textField.text = @"99999";
        }

        /*
         * 不能输入.0-9以外的字符。
         * 设置输入框输入的内容格式
         * 只能有一个小数点
         * 小数点后最多能输入两位
         * 如果第一位是.则前面加上0.
         * 如果第一位是0则后面必须输入点，否则不能输入。
         */

        // 判断是否有小数点
        if ([textField.text containsString:@"."]) {
            self.isHaveDian = YES;
        }else{
            self.isHaveDian = NO;
        }

//        if ([textField.text isEqualToString:@"0"]) {
//            return NO;
//        }
        if (string.length > 0) {

            //当前输入的字符
            unichar single = [string characterAtIndex:0];
            //BXLog(@"single = %c",single);

            // 不能输入.0-9以外的字符
            if (!((single >= '0' && single <= '9') || single == '.'))
            {
                //[MBProgressHUD bwm_showTitle:@"您的输入格式不正确" toView:self hideAfter:1.0];
                return NO;
            }

            // 只能有一个小数点
            if (self.isHaveDian && single == '.') {
                //[MBProgressHUD bwm_showTitle:@"最多只能输入一个小数点" toView:self hideAfter:1.0];
                return NO;
            }

            // 如果第一位是.则前面加上0.
            if ((textField.text.length == 0) && (single == '.')) {
                return NO;
            }

            // 如果第一位是0则后面必须输入点，否则不能输入。
            if ([textField.text hasPrefix:@"0"]) {
                if (textField.text.length > 1) {
                    NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1, 1)];
                    if (![secondStr isEqualToString:@"."]) {
                        //[MBProgressHUD bwm_showTitle:@"第二个字符需要是小数点" toView:self hideAfter:1.0];
                        return NO;
                    }
                }else{
                    if (![string isEqualToString:@"."]) {
                        //[MBProgressHUD bwm_showTitle:@"第二个字符需要是小数点" toView:self hideAfter:1.0];
                        return NO;
                    }
                }
            }

            // 小数点后最多能输入两位
            if (self.isHaveDian) {
                NSRange ran = [textField.text rangeOfString:@"."];
                // 由于range.location是NSUInteger类型的，所以这里不能通过(range.location - ran.location)>2来判断
                if (range.location > ran.location) {
                    if ([textField.text pathExtension].length > 1) {
                        //[MBProgressHUD bwm_showTitle:@"小数点后最多有两位小数" toView:self hideAfter:1.0];
                        return NO;
                    }
                }
            }
        }
        
    }

    return YES;
}


@end
